package main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import common.AntiCrack;

public class doActivate implements ActionListener {
	private final File f = new File("xycf.!mv");
	private JFrame jf;
	Activation act;
	private final JTextField jtf;
	private String title;
	private JTextField jtf2;
	private JLabel jlab1;

	public doActivate(int type, String code) {
		try {
			act = new Activation();
			title = type == 1 ? "激活" : "续期";
			String button = type == 1 ? "确定激活" : "确定续期";
			String jlabel = type == 1 ? "机器ID码:" : "原激活卡:";
			jf = new JFrame("激活窗口-" + title);
			jlab1 = new JLabel(jlabel, JLabel.RIGHT);
			jlab1.setBounds(0, 10, 60, 20);
			jtf2 = new JTextField(code);
			jtf2.setBounds(70, 10, 250, 20);
			JLabel jlab3 = new JLabel("新激活卡:", JLabel.RIGHT);
			jlab3.setBounds(0, 40, 60, 20);
			JButton jb = new JButton(button);
			jb.setBounds(100, 70, 90, 20);
			jb.addActionListener(this);
			jb.requestFocus();
			jtf = new JTextField();
			jtf.setForeground(Color.GRAY);
			jtf.setBounds(70, 40, 250, 20);
			jtf.addKeyListener(new KeyListener() {
				public void keyTyped(KeyEvent e) {
					if (e.getKeyChar() == '\n')
						doCheck();
				}

				public void keyReleased(KeyEvent e) {
				}

				public void keyPressed(KeyEvent e) {
				}
			});
			jtf.addFocusListener(new FocusListener() {
				public void focusLost(FocusEvent e) {
					if (jtf.getText().equals("")) {
						jtf.setText("此处输入新激活码");
						jtf.setForeground(Color.GRAY);
					}
				}

				public void focusGained(FocusEvent e) {
					if (jtf.getText().equals("此处输入新激活码")) {
						jtf.setText("");
						jtf.setForeground(Color.BLACK);
					}
				}
			});
			JButton copy = new JButton("复制卡/码");
			copy.setActionCommand("copy");
			copy.addActionListener(this);
			copy.setBounds(200,70,120,20);
			jf.add(jlab1);
			jf.add(jtf2);
			jf.add(jlab3);
			jf.add(jtf);
			jf.add(jb);
			jf.add(copy);
			jf.setBounds(200, 100, 330, 130);
			jf.setLayout(new BorderLayout());
			jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jf.setResizable(false);
			jf.setVisible(true);
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("copy")) {
			StringSelection contents = new StringSelection(jlab1.getText() + jtf2.getText());
			Toolkit.getDefaultToolkit().getSystemClipboard().setContents(contents, null);
		} else
			doCheck();
	}

	private void doCheck() {
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		String a;

		String msg = act.deCard(jtf.getText());
		if (null != msg) {
			try {
				fis = new FileInputStream(f);
				isr = new InputStreamReader(fis);
				br = new BufferedReader(isr);
				a = br.readLine();
				br.close();
				isr.close();
				fis.close();
				fos = new FileOutputStream(f);
				osw = new OutputStreamWriter(fos);
				bw = new BufferedWriter(osw);
				bw.write(a + "\r\n");
				bw.write(jtf.getText());
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "激活失败，请以管理员身份重新运行！");
			} finally {
				try {
					bw.flush();
					bw.close();
					osw.close();
					osw.close();
				} catch (Exception e) {
				}
				if (title.equals("激活"))
					msg = msg + "\n请重新启动";
				JOptionPane.showMessageDialog(null, title + "成功！\n有效期至" + msg);
				if (title.equals("激活"))
					System.exit(0);
				jf.setVisible(false);
			}
		}
	}
}
